package com.wuhan.template.module.home.activity;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.wuhan.template.R;
import com.wuhan.template.base.BaseActivity;
import com.wuhan.template.base.BaseApplication;
import com.wuhan.template.constant.Urls;
import com.wuhan.template.module.home.adapter.DianBoItemBaseAdapter;
import com.wuhan.template.module.home.entity.DianBoItemEntity;
import com.wuhan.template.utils.EventMessage;
import com.wuhan.template.utils.OkHttpUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;

public class DianBoItemActivity extends BaseActivity {

    private String imageUrl;
    private String column_id;
    private String content;
    @BindView(R.id.jiaoShi_image)
    protected ImageView imageView;

    @BindView(R.id.dianboItem_title)
    protected TextView title;

    @BindView(R.id.jianjie_text)
    protected TextView jianJie;

    private  Boolean flag = true;

    @BindView(R.id.dianbo_itemPull)
    protected PullToRefreshListView listView;
    private DianBoItemBaseAdapter baseAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_dian_bo_item;
    }

    @Override
    protected void initViews() {
        Intent intent = getIntent();
        imageUrl = intent.getStringExtra("imageUrl");
        column_id = intent.getStringExtra("column_id");
        content = intent.getStringExtra("text");
        title.setText(intent.getStringExtra("title"));
        jianJie.setText(content);
        final ImageLoader loader = ((BaseApplication) (getApplicationContext())).getImageLoader();
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .cacheInMemory(true).cacheOnDisk(true).build();
        loader.displayImage(imageUrl, imageView, options);

        baseAdapter = new DianBoItemBaseAdapter(this, list);
        listView.setAdapter(baseAdapter);

        listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                list.clear();
                offset = 0;
                loadData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                offset += 20;
                loadData();
            }
        });

        jianJie.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if(flag){
                    flag = false;
                    jianJie.setEllipsize(null); // 展开
                    jianJie.setSingleLine(flag);

                }else{
                    flag = true;
                    jianJie.setEllipsize(TextUtils.TruncateAt.END); // 收缩
                    jianJie.setSingleLine(flag);
                }
            }});



    }

    private int offset = 0;

    @Override
    protected void loadData() {
        OkHttpUtils.get(Urls.DIANBoITEM + "&count=20&column_id=" + column_id + "&offset=" + offset, null);
    }

    private List<DianBoItemEntity> list = new ArrayList<>();

    @Override
    protected void postEventResult(@NonNull EventMessage message) {
        if (message.type.equals(Urls.DIANBoITEM + "&count=20&column_id=" + column_id + "&offset=" + offset)) {
            Gson gson = new Gson();
            DianBoItemEntity[] dianBoItem = gson.fromJson(message.result, DianBoItemEntity[].class);
            list.addAll(Arrays.asList(dianBoItem));

            baseAdapter.notifyDataSetChanged();
            listView.onRefreshComplete();
        }
    }
}
